//取 url 参数函数
function query(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)return unescape(r[2]);
    return "";
}

function getMenu(id, menua) {
    var menu = null;
    for (var i = 0; i < menua.length; i++) {
        if (menua[i].id == id) {
            menu = menua[i];
            break;
        }
    }
    return menu;
}

$(function () {
    var roletype = parseInt(query("type"));
    var stateStr = "";
    switch (roletype) {
        case 0:
            stateStr = "部门";
            break;
        case 1:
            stateStr = "门店";
            break;
        case 2:
            stateStr = "推广人员";
            break;
        case 3:
            stateStr = "推广组长";
            break;
        case 4:
            stateStr = "客服人员";
            break;
        case 5:
            stateStr = "客服组长";
            break;
        case 6:
            stateStr = "客服主管";
            break;
    }
    console.log(stateStr);
    $("#rolename").html(stateStr);
    var url="apis/cuswx/getAllMenuPermissions";
    var data = {};
    var jsonData = JSON.stringify(data);
    //获取菜单，这个需要同步，否则下面获取数据没法赋值
    $.ajax({
        url: url,
        type: "post",
        data: jsonData,
        dataType: 'json',
        async: false,
        contentType : 'application/json',
        success: function (data) {
            console.log(data);
            return;
            if (data.httpCode == 200) {
                $("#roleContentList").empty();
                var rolelist = data.data;

                var menuArray = [];
                for (var i = 0; i < rolelist.length; i++) {
                    if (rolelist[i].isShow == 0) continue;
                    if (rolelist[i].parentId == 0) {
                        //一级节点
                        var sHtmlParent;
                        sHtmlParent = "";
                        sHtmlParent += "<div class=\"tab_box\">";
                        sHtmlParent += "<h1 style='margin-top: 40px' id=\"type-tit" + rolelist[i].id + "\">" +
                            "<input style='vertical-align:middle;' type=\"checkbox\" onclick='checkParent(this)'>" +
                            "<span style='vertical-align:middle;padding-left: 5px; line-height: 20px !important;'>" + rolelist[i].menuName + "</span></h1>";
                        sHtmlParent += "<div id=\"neiyecount" + rolelist[i].id + "\" class=\"text-single\">";
                        sHtmlParent += "<div id=\"type" + rolelist[i].id + "\" class=\"spec-top\">";
                        sHtmlParent += "<div class=\"height1\"></div>";

                        var menu = {
                            "id": rolelist[i].id,
                            "html": sHtmlParent
                        };
                        menuArray.push(menu);
                    }
                }

                for (var i = 0; i < rolelist.length; i++) {
                    if (rolelist[i].isShow == 0) continue;
                    if (rolelist[i].parentId != 0) {
                        //二级节点
                        var menu = getMenu(rolelist[i].parentId, menuArray);
                        if (menu != null) {
                            var sHtmlMenu;
                            sHtmlMenu = menu.html;

                            sHtmlMenu += "<div class=\"model_wrap\" style='width: 700px; border-bottom: 1px dashed #7c7c7c'>";
                            sHtmlMenu += "<span class=\"pop_tit floatl\" style='margin-top: 15px; text-align: left; width: 150px !important;'>" +
                                "<input id=\"checkRoleItem" + rolelist[i].id + "\" parentid=\"" + rolelist[i].parentId + "\" style='vertical-align:middle;' type=\"checkbox\" onclick='checkChild(this)'>" + rolelist[i].menuName + "</span>";
                            sHtmlMenu += "<ul style='margin-top: 10px' id=\"menuOpItems" + rolelist[i].id + "\"  class=\"color_ul floatr\">" + "</ul>";
                            sHtmlMenu += "</div>";

                            menu.html = sHtmlMenu;
                        }
                    }
                }

                for (var i = 0; i < menuArray.length; i++) {
                    var menu = menuArray[i];
                    var sHtmlMenu;
                    sHtmlMenu = menu.html;
                    sHtmlMenu += "</div></div></div>";
                    menu.html = sHtmlMenu;

                    $("#roleContentList").append(menu.html);
                }
            }
        }
    });

})